package com.kyzh.core.pager.weal.share

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.text.Html
import android.util.Base64
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setMargins
import com.gushenge.core.beans.ShareNewBean
import com.gushenge.core.requests.UserRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.pager.weal.share.ShareHistoryActivity
import com.kyzh.core.databinding.ActivityShare2Binding
import com.kyzh.core.databinding.ItemShareLiuchengBinding
import com.kyzh.core.databinding.ItemShareMsgBinding
import com.kyzh.core.utils.copy
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.time

class ShareNewActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, ShareNewActivity::class.java)
            context.startActivity(starter)
        }
    }
    private val liuChengAdapter = LiuChengAdapter()
    private val msgChengAdapter = MessageAdapter()
    private lateinit var db: ActivityShare2Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this,R.layout.activity_share2)
        initView()
        initData()
    }

    private fun initView() {
        db.ImgHistory.setMargins(0, AView.getStatusBarHeight(this),0,0)

        db.liuchengRev.adapter = liuChengAdapter
        db.revMsg.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
        db.revMsg.adapter =msgChengAdapter
//        db.ImgHistory.setOnClickListener {
//
//        }
        db.imgSend.setOnClickListener {
            ShareHistoryNewActivity.start(this)
        }
        db.withrecord.setOnClickListener {
            startActivity<ShareHistoryActivity>()
        }
    }

    private fun initData() {

        UserRequest.shareFriend {
            db.data = this
            liuChengAdapter.setNewInstance(this.liucheng)
            val liucheng  = arrayListOf(
                ShareNewBean.Liucheng(title ="邀请好友",icon =  num)
                ,ShareNewBean.Liucheng(title ="获得平台币",icon =  coin)
                ,ShareNewBean.Liucheng(title ="获得积分",icon =  points))
            msgChengAdapter.setNewInstance(liucheng)
            db.titleView.setOnMoreOnClickListener {
                val toString = String(Base64.decode(note, Base64.NO_PADDING))
                AlertDialog.Builder(this@ShareNewActivity, R.style.kyzhGuestLoginDialog)
                    .setMessage(Html.fromHtml(  toString))
                    .setPositiveButton("关闭") { a, _ ->
                        a.dismiss()
                    }
                    .create().show()
            }
            db.tvCopy.setOnClickListener {
                "复制成功".toast()
                link.copy()
            }
            db.code.setOnLongClickListener {
//                ImageUtils.get
//                db.code.drawable.
                Thread {
                    val myBitmap = Glide.with(applicationContext).asBitmap()
                        .load(qrcode)
                        .centerCrop()
                        .into(500, 500)
                        .get()
                    MediaStore.Images.Media.insertImage(contentResolver, myBitmap, time, "")
                    runOnUiThread {
                        "保存成功".toast()
                    }
                }.start()

                true
            }

        }
    }


    private inner class LiuChengAdapter :BaseQuickAdapter<ShareNewBean.Liucheng,BaseDataBindingHolder<ItemShareLiuchengBinding>>(R.layout.item_share_liucheng){
        override fun convert(
            holder: BaseDataBindingHolder<ItemShareLiuchengBinding>,
            item: ShareNewBean.Liucheng
        ) {
            holder.dataBinding?.data = item
        }
//        aesEcb
    }

    private inner class MessageAdapter :BaseQuickAdapter<ShareNewBean.Liucheng,BaseDataBindingHolder<ItemShareMsgBinding>>(R.layout.item_share_msg){
        override fun convert(
            holder: BaseDataBindingHolder<ItemShareMsgBinding>,
            item: ShareNewBean.Liucheng
        ) {
            holder.dataBinding?.data = item
        }
    }



}